<?php

namespace App\Models;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Symfony\Component\Finder\Iterator\CustomFilterIterator;
use DateTimeInterface;

/**
 * App\Models\BlogUser
 *
 * @property int $id 用户表
 * @property string $username 用户名
 * @property string $password 密码
 * @property string|null $sex 性别
 * @property string|null $images 头像
 * @property \Illuminate\Support\Carbon|null $created_at 创建日期
 * @property \Illuminate\Support\Carbon|null $updated_at 修改日期
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser query()
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereImages($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereSex($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereUsername($value)
 * @mixin \Eloquent
 * @property-read \App\Models\BlogProfile|null $profile
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\BlogBook[] $book
 * @property-read int|null $book_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\BlogRole[] $role
 * @property-read int|null $role_count
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @property-read \Illuminate\Database\Eloquent\Collection|\Laravel\Sanctum\PersonalAccessToken[] $tokens
 * @property-read int|null $tokens_count
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser filter(array $input = [], $filter = null)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser paginateFilter($perPage = null, $columns = [], $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser simplePaginateFilter($perPage = null, $columns = [], $pageName = 'page', $page = null)
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereBeginsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereEndsWith($column, $value, $boolean = 'and')
 * @method static \Illuminate\Database\Eloquent\Builder|BlogUser whereLike($column, $value, $boolean = 'and')
 */
class BlogUser extends Model
{
    use HasFactory,  HasApiTokens, Notifiable, Filterable;

    //开放所有权限
    protected $guarded = ['id'];

    //隐藏密码字段
    protected $hidden = ['password'];

    //访问器：给images字段添加前缀
    public function getImagesAttribute($pic)
    {
        return 'https://www.blog.avatar'.$pic;
    }

    //修改器：将密码进行加密后再保存
    public function setPasswordAttribute($password)
    {
        return $this->attributes['password'] = \Hash::make($password);
    }

    //解决日期返回格式问题
    public function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function modelFilter()
    {
        return $this->provideFilter(CustomFilterIterator::class);
    }

    //一对多反向关联
    public function profile()
    {
        return $this->belongsTo(BlogProfile::class,'id','uid');
    }

    //正向一对一关联
//    public function profile()
//    {
//        return $this->hasOne(BlogProfile::class,'uid','id');
//    }

    //正向一对多关联
    public function book()
    {
        return $this->hasMany(BlogBook::class,'uid','id');
    }

    //正向多对多关联
    public function role()
    {
        return $this->belongsToMany(BlogRole::class,'blog_role_users','uid','tid','id')
            ->withPivot('detail');
//            ->wherePivot('id','>=','1');  //筛选中间表的数据
    }


}
